package com.nl.controller.web.controller;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.AdcFunction;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.Sms_DT;
import com.nl.controller.web.bean.TreeInfo;
import com.nl.controller.web.service.LoginService;
import com.nl.controller.web.util.SendSms;
import com.nl.controller.xuyi.service.XuYiStationService;

import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private CommonService commonService;
    
    
    @PostMapping("login")
    public Response login(
            HttpSession session,
            @RequestParam("msisdn") String msisdn,
            @RequestParam("password") String password,
            @RequestParam("code") String code
    ) {
    	if ("".equals(code)  || null == code ) {
    		return ResponseUtil.error(-1,"请输入验证码");
		}
        SmsUsers user = loginService.login(msisdn, password);
        session.setAttribute("user", user);
        log.info("登录参数：{}", msisdn + " " + password);
        log.debug("登录返回数据：{}", user.toString());
        return ResponseUtil.sucess(user);
    }
    
    @PostMapping("getLogin")
    public Response getLogin(
            HttpSession session
    ) {
    	String msisdn="15150570249";
    	String password="123456";
        SmsUsers user = loginService.login(msisdn, password);
        session.setAttribute("user", user);
        session.setMaxInactiveInterval(30 * 60);
        log.info("登录参数：{}", msisdn + " " + password);
        log.debug("登录返回数据：{}", user.toString());
        return ResponseUtil.sucess(user);
    }

    @PostMapping("menu")
    public Response getMenu(
            HttpSession session
    ) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String id = user.getUserId();
        List list = loginService.getMenu(id);
        log.info("获取菜单参数：{}", user.toString());
        log.debug("获取菜单返回值：{}", list.size());
        return ResponseUtil.sucess(list);
    }

    @PostMapping("functions")
    public List<TreeInfo> getFunctions() {
        List list = loginService.getFuncitons();
        log.info("获取权限树：{}");
        log.debug("获取权限树返回值：{}", list.size());
        return list;
    }

    @PostMapping("/login/sendCode")
    public Response sendCode(
            HttpSession session, @RequestParam("msisdn") String msisdn, @RequestParam("flag") String flag,
            @RequestParam("password") String password
    ) {
    	SmsUsers user = loginService.login(msisdn, password);
        session.setAttribute("user", user);
        log.info("登录参数：{}", msisdn + " " + password);
        log.debug("登录返回数据：{}", user.toString());
        String number = AdcFunction.getSixNumber(6);
        //组织短信内容
        String smscontent;
        if ("reset".equals(flag)) {
            smscontent = "【江苏省应急平台】您好！欢迎使用应急信息发布管理平台，您的密码重置短信验证码是：" + number + "，有效时长为5分钟,请妥善保管，请勿告知他人。";
        } else {
            smscontent = "【江苏省应急平台】您好！欢迎登录应急信息发布管理平台，您的短信验证码是：" + number + "，有效时长为5分钟,请妥善保管，请勿告知他人。";
        }
        System.out.println("smscontent = " + smscontent);
        System.out.println("moblie_number = " + msisdn);
        //设置验证码开始获取时间
        session.setAttribute("sendTime", new Long(System.currentTimeMillis()));
        session.setAttribute("number", number);

        int result = loginService.sendCode(msisdn,smscontent);
        log.info("发送信息参数：{}", msisdn);
        log.debug("发送验证码返回值：{}", result);
        
        return ResponseUtil.sucess(result);
    }

    

    @PostMapping("/login/judgeMentCode")
    public String judgeMentCode(HttpSession session, @RequestParam("code") String code) {
        Object number = session.getAttribute("number");
        long time = 0;
        time = (long) session.getAttribute("sendTime");
        long currentTime = System.currentTimeMillis();
        //验证登录超时
        log.info("验证登录超时参数：{}", code);
        if ((currentTime - time) >= (5 * 60 * 1000)) {
            return "long";
        } else {
            if (code.equals(number.toString())) {
                return "success";
            } else {
                return "fail";
            }
        }
    }

}
